package com.pucong.gitee.database.backup.configuration.authorize;

import com.pucong.gitee.database.backup.common.entity.sys.authorize.AuthorizeServerInfo;
import lombok.extern.slf4j.Slf4j;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.Locale;

/**
 * 获取本机信息
 */
@Slf4j
public class NetworkUtils {

    /**
     * 获取本地IP列表（针对多网卡情况）
     * @return
     */
    public static AuthorizeServerInfo getLocalInetMac() {
        AuthorizeServerInfo ipMacInfo = null;
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces
                        .nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface
                        .getInetAddresses();

                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    ipMacInfo = pickInetAddress(inetAddress, networkInterface);
                    if (ipMacInfo != null) {
                        log.info("IP-MAC-1",ipMacInfo);
                        return ipMacInfo;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static AuthorizeServerInfo pickInetAddress(InetAddress inetAddress,
                                                       NetworkInterface ni) {
        try {
            String name = ni.getDisplayName();
            if (name.contains("Adapter")
                    || name.contains("Virtual") || name.contains("VMnet") || name.contains("#")) {
                return null;
            }
            if (ni.isVirtual() || !ni.isUp() || !ni.supportsMulticast()) {
                return null;
            }

            if (inetAddress.isSiteLocalAddress()) {
                Formatter formatter = new Formatter();
                String sMAC = null;
                byte[] macBuf = ni.getHardwareAddress();
                for (int i = 0; i < macBuf.length; i++) {
                    sMAC = formatter.format(Locale.getDefault(), "%02X%s",
                            macBuf[i], (i < macBuf.length - 1) ? "-" : "")
                            .toString();
                }
                formatter.close();
                AuthorizeServerInfo authorizeServerInfo = new AuthorizeServerInfo();
                authorizeServerInfo.setHostname(inetAddress.getHostName());
                authorizeServerInfo.setIp(inetAddress.getHostAddress());
                authorizeServerInfo.setIpnet(inetAddressTypeName(inetAddress));
                authorizeServerInfo.setOs(System.getProperty("os.name"));
                authorizeServerInfo.setMac(sMAC);
                authorizeServerInfo.setCpuArch(System.getProperty("os.arch"));
                authorizeServerInfo.setNetworkArch(ni.getDisplayName());
                return authorizeServerInfo;
            }

        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String inetAddressTypeName(InetAddress inetAddress) {
        return (inetAddress instanceof Inet4Address) ? "ipv4" : "ipv6";
    }

    public static void main(String[] args) {
        AuthorizeServerInfo localInetMac = getLocalInetMac();
        System.out.println(localInetMac);
    }

}
